From 6cbd479e30dc414af9b892e96412920a753391cd Mon Sep 17 00:00:00 2001 From: Keir Fraser Date: Tue, 18 May 2010 11:38:12 +0100 Subject: [PATCH] xl: allow scaling suffix on memory sizes in mem-set and mem-max Allow mem-set and mem-max to take 'b', 'k', 'm', 'g' and 't' as scaling suffixes for bytes, kilobytes, mega, etc. An unadorned number is still treated as kilobytes so no existing users should be affected. Signed-off-by: Jeremy Fitzhardinge Signed-off-by: Keir Fraser --- tools/libxl/xl_cmdimpl.c | 47 +++++++++++++++++++++++++++++++-------- tools/libxl/xl_cmdtable.c | 8 +++++-- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c index afadeda1d1..b5d2eba022 100644 --- a/tools/libxl/xl_cmdimpl.c +++ b/tools/libxl/xl_cmdimpl.c @@ -1200,16 +1200,45 @@ void help(char *command) } } -int set_memory_max(char *p, char *mem) +static int64_t parse_mem_size_kb(char *mem) { char *endptr; - uint32_t memorykb; + int64_t kbytes; + + kbytes = strtoll(mem, &endptr, 10); + + if (strlen(endptr) > 1) + return -1; + + switch (tolower(*endptr)) { + case 't': + kbytes <<= 10; + case 'g': + kbytes <<= 10; + case 'm': + kbytes <<= 10; + case '\0': + case 'k': + break; + case 'b': + kbytes >>= 10; + break; + default: + return -1; + } + + return kbytes; +} + +int set_memory_max(char *p, char *mem) +{ + int64_t memorykb; int rc; find_domain(p); - memorykb = strtoul(mem, &endptr, 10); - if (*endptr != '\0') { + memorykb = parse_mem_size_kb(mem); + if (memorykb == -1) { fprintf(stderr, "invalid memory size: %s\n", mem); exit(3); } @@ -1255,17 +1284,17 @@ int main_memmax(int argc, char **argv) void set_memory_target(char *p, char *mem) { - char *endptr; - uint32_t memorykb; + long long int memorykb; find_domain(p); - memorykb = strtoul(mem, &endptr, 10); - if (*endptr != '\0') { + memorykb = parse_mem_size_kb(mem); + if (memorykb == -1) { fprintf(stderr, "invalid memory size: %s\n", mem); exit(3); } - printf("setting domid %d memory to : %d\n", domid, memorykb); + + printf("setting domid %d memory to : %lld\n", domid, memorykb); libxl_set_memory_target(&ctx, domid, memorykb, /* enforce */ 1); } diff --git a/tools/libxl/xl_cmdtable.c b/tools/libxl/xl_cmdtable.c index 68b8279b92..82cf2ca722 100644 --- a/tools/libxl/xl_cmdtable.c +++ b/tools/libxl/xl_cmdtable.c @@ -110,12 +110,16 @@ struct cmd_spec cmd_table[] = { }, { "mem-max", &main_memmax, - "Set the maximum amount reservation for a domain", + "Set the maximum amount reservation for a domain.\n" + "Units default to kilobytes, but can be suffixed with\n" + "'b' (bytes), 'k' (KB), 'm' (MB), 'g' (GB) or 't' (TB)", " ", }, { "mem-set", &main_memset, - "Set the current memory usage for a domain", + "Set the current memory usage for a domain.\n" + "Units default to kilobytes, but can be suffixed with\n" + "'b' (bytes), 'k' (KB), 'm' (MB), 'g' (GB) or 't' (TB)", " ", }, { "button-press", -- 2.30.2